package com.ourfox.a4.ui.controller.usermanager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourfox.a4.commons.util.EntityReflect;
import com.ourfox.a4.commons.util.JsonUtil;
import com.ourfox.a4.ui.bean.usermanager.User;
import com.ourfox.a4.ui.exception.UserManagerException;

@Controller
@RequestMapping("/user")
public class UserController {
	
	private static List<User> userList = new ArrayList<User>();
	
	static
	{
		for (int i = 0; i < 5; i++) {
			User user = new User();
			user.setUserId(i + "");
			user.setUserName("test_" + i);
			user.setRole("role_" + i);
			user.setDescription("user test");
			user.setStatus("1");
			userList.add(user);
		}
	}

	/**
	 * 用户列表查询
	 * 
	 * @param request
	 *            请求
	 * @param response
	 *            相应
	 * @return 返回数据集合
	 * @throws Exception
	 *             异常
	 */
	@RequestMapping("search.do")
	public @ResponseBody
	String search(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		limit = limit + start;
		
		if (limit > userList.size())
		{
			limit = userList.size();
		}
		// 测试数据后续删除
		JSONObject dataObj = JsonUtil.toJSon(userList.subList(start, limit), userList.size(),
				new SimpleDateFormat("yyyy-MM-dd"));

		return dataObj.toString();
	}

	/**
	 * 插入用户信息
	 * 
	 * @param request
	 *            请求
	 * @param response
	 *            相应
	 * @return
	 * @throws Exception
	 *             异常
	 */
	@RequestMapping("insert.do")
	public @ResponseBody
	String insert(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONObject obj = new JSONObject();
		obj.put("success", true);
		User user = (User) EntityReflect.createObjectFromRequest(request,
				User.class);
		userList.add(user);
		obj.put("result", "success");
		return obj.toString();
	}
	
	/**
	 * 删除用户信息
	 * @param request 请求
	 * @param response 相应
	 * @return
	 */
	@RequestMapping("delete.do")
	public @ResponseBody String delete(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		String userIds = request.getParameter("userIds");
		//resourcesDao.deleteById(Integer.parseInt(id));
		/*try {
			int num = 1 / 0;
		} catch (Exception e) {
			obj.put("result", "error");
			obj.put("info", e.getMessage());
			
		}*/
		
		obj.put("result","success");
		return obj.toString();  
	}
	
	
	/**
	 * 
	 * 异常测试后续会删除
	 * 
	 */
	@RequestMapping("exception.do")
	public @ResponseBody String exception(HttpServletRequest request, HttpServletResponse response)
	{
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		
		try {
			int num = 1 / 0;
		} catch (Exception e) {
			obj.put("result","error");
			
			obj.put("info", new UserManagerException(10001).getMessage());
		}
		
		return obj.toString();  
	}

}
